package com.example.s3demo.utils

import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes


object CommonUtil {
    fun newGuid(): String{
            val dateStr = DateUtil.dateTagToSecond()
            var randomStr = java.util.UUID.randomUUID().toString()
            try {
                randomStr = EncryptUtil.encryptByMD5(randomStr).lowercase(java.util.Locale.getDefault())
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
            return dateStr + randomStr
        }

    fun apiPath(): String {
            val requestAttributes: ServletRequestAttributes =
                RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
            val request: HttpServletRequest = requestAttributes.getRequest()
            val requestURL: String = request.getRequestURL().toString()
            val scheme: String = request.getScheme()
            val servaerName: String = request.getServerName()
            val port: Int = request.getServerPort()
            val rootPageURL = "$scheme://$servaerName:$port/"
            return rootPageURL
        }
}
